﻿using System;

using xfsale.lib;
using xfsale.packet;

namespace xfsale.ui.acct
{
    public partial class FrmTransAcctOpen : FrmBase
    {
        public FrmTransAcctOpen()
        {
            InitializeComponent();
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            string password = GetTradePassword();

            try
            {
                //填充请求数据
                TransAcctOpenRequest request = new TransAcctOpenRequest();
                Bind(request);

                int deliverWay = 0x00;
                if (ChkDeliverPost.Checked)
                    deliverWay |= 0x80;
                if (ChkDeliverFax.Checked)
                    deliverWay |= 0x40;
                if (ChkDeliverFax.Checked)
                    deliverWay |= 0x20;

                int tradingMethod = 0x80;
                if (ChkTradeInternet.Checked)
                    tradingMethod |= 0x40;
                if (ChkTradeFax.Checked)
                    tradingMethod |= 0x20;
                if (ChkTradeTel.Checked)
                    tradingMethod |= 0x10;
                if (ChkTradeYJT.Checked)
                    tradingMethod |= 0x08;

                request.nationality = "086";
                request.tradePassword = password;
                request.deliverWay = Convert.ToString(deliverWay, 2).PadRight(8);
                request.tradingMethod = Convert.ToString(tradingMethod, 2).PadRight(8);
                request.bankAcctVerified = "1";
                

                //调用服务
                TransAcctOpenResponse response = (TransAcctOpenResponse)CallService(request);

                //处理应答结果
                if (response.returnCode == "0000")
                {
                    txtOrderSheetNo.Text = response.orderSerialNo;
                    SysMessage.Info("交易账户开户成功！\n\n交易帐号:{0}", response.transAccountID);
                }
                else
                {
                    SysMessage.Error(response.returnMsg);
                }
            }
            catch (Exception ex)
            {
                SysMessage.Error(ex.Message);
            }
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            CloseFrame();
        }

        private void BtnClear_Click(object sender, EventArgs e)
        {
            Reset();

            RadCustTypeIndi.Checked = true;
            RadCustTypeInst.Checked = false;
            SetCtrlValue(CmbCustType, "1");
            InitCtrl(CmbCertType, "IndiCertType");
        }

        private void RadInvst_Click(object sender, EventArgs e)
        {
            if (sender == RadCustTypeIndi) //选择个人
            {
                SetCtrlValue(CmbCustType, "1");
                InitCtrl(CmbCertType, "IndiCertType");
            }
            else if (sender == RadCustTypeInst) //选择机构
            {
                SetCtrlValue(CmbCustType, "0");
                InitCtrl(CmbCertType, "InstCertType");
            }
        }

        private void FrmAcctTransOpen_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
            }
        }

        private void TxtInvstName_TextChanged(object sender, EventArgs e)
        {
            TxtCustNameAbbr.Text = TxtCustName.Text;
            TxtBankAcctName.Text = TxtCustName.Text;
        }
    }
}
